package com.wei.web.middleware.httpfilter.utils;


import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.servlet.config.annotation.CorsRegistration;

import java.util.Arrays;


/**
 * @author Chuanding.Si
 * @date 2021/9/2 4:50 PM
 */
public class ZvaCorsRegistration extends CorsRegistration {
    private final String pathPattern;
    private final ZvaCorsConfiguration config;

    public ZvaCorsRegistration(String pathPattern) {
        super(pathPattern);
        this.pathPattern = pathPattern;
        this.config = (new ZvaCorsConfiguration()).applyPermitDefaultValues();
    }

    @Override
    public CorsRegistration allowedOrigins(String... origins) {
        this.config.setAllowedOrigins(Arrays.asList(origins));
        return this;
    }

    @Override
    public CorsRegistration allowedMethods(String... methods) {
        this.config.setAllowedMethods(Arrays.asList(methods));
        return this;
    }

    @Override
    public CorsRegistration allowedHeaders(String... headers) {
        this.config.setAllowedHeaders(Arrays.asList(headers));
        return this;
    }

    @Override
    public CorsRegistration exposedHeaders(String... headers) {
        this.config.setExposedHeaders(Arrays.asList(headers));
        return this;
    }

    @Override
    public CorsRegistration allowCredentials(boolean allowCredentials) {
        this.config.setAllowCredentials(allowCredentials);
        return this;
    }

    @Override
    public CorsRegistration maxAge(long maxAge) {
        this.config.setMaxAge(maxAge);
        return this;
    }

    @Override
    protected String getPathPattern() {
        return this.pathPattern;
    }

    @Override
    protected CorsConfiguration getCorsConfiguration() {
        return this.config;
    }
}
